<?php

import('com.yamvc.view.Mediator');
import("com.simpleApplication.events.AuthEvent");
import("com.yamvc.validators.StringValidator");

class AuthPanelMediator extends Mediator {
	/**
	 * Enter description here...
	 *
	 * @var StringValidator
	 */
	private $_loginValidator;
	/**
	 * Enter description here...
	 *
	 * @var StringValidator
	 */
	private $_passwordValidator;
	public function __construct(ITemplate $template){
		parent::__construct($template);
		$this->addEventListener(ApplicationEvent::ACTION_DISPATCHED,new EventListener($this,"init"));
		$this->addEventListener(AuthEvent::AUTH,new EventListener($this,"authEventHandler"));
		$this->addEventListener(AuthEvent::LOGOUT,new EventListener($this,"logoutEventHandler"));
	}
	
	//public function eventDispatched
	
	public function logoutEventHandler(AuthEvent $e){
		Application::getAppInstance()->getUser()->logout();
		Utils::redirect(Application::getAppInstance()->getCurrentAction());
	}
	
	public function authEventHandler(AuthEvent $e){
		$this->_template->putVariable("errorString","ERROR!!");
		try{
			$login = Request::instance()->post->login;
			$pwd = Request::instance()->post->password;
		}catch (NullPointerException $e){
			$this->_template->putVariable("errorString","Hack?");
			return;
		}
		if ($this->_loginValidator->validate($login)==false||$this->_passwordValidator->validate($pwd)==false){
			$this->_template->putVariable("errorString",$this->_loginValidator->getErrorString()."\n <br />".$this->_passwordValidator->getErrorString());
			return;
		}
		try{
			$user = new User($login);
			$user->auth($pwd);
		}catch (UserNotFoundException $e){
			$this->_template->putVariable("errorString","User no found!!");
			return;
		}catch (WrongPasswordException $e){
			$this->_template->putVariable("errorString","Wrong Password!!");
			return;
		}
		//$this->_template->putVariable("login",$login);
		Utils::redirect(Application::getAppInstance()->getCurrentAction());
	}
	
	public function init(ApplicationEvent $e){
		$user=Application::getAppInstance()->getUser();
		if($user->isAuthOk()==false){
			$this->_template->putVariable("auth",false);
			$this->_loginValidator = new StringValidator();
			$this->_loginValidator->setAllowedChars("abcd")->setMaxLength(15)->setMinLength(4);
			$this->_passwordValidator = new StringValidator();
			$this->_passwordValidator->setAllowedChars("abcd")->setMaxLength(15)->setMinLength(5);
			$this->addEventListener(AuthEvent::AUTH,new EventListener($this,"authEventHandler"));
		}else{
			$this->_template->putVariable("auth",true);
			$this->_template->putVariable("login",$user->getLogin());
			$this->addEventListener(AuthEvent::LOGOUT,new EventListener($this,"logoutEventHandler"));
		}
		$this->_template->createUrl("url",$e->getApplication()->getCurrentAction(),"AuthEvent","authEvent");
		$this->_template->createUrl("logoutUrl",Application::getAppInstance()->getCurrentAction(),"AuthEvent","logoutEvent");
	}
}

?>